﻿using System;
using System.Collections.Generic;
using SortOf;

namespace ExampleRBTree
{
    class Program
    {
        private static RBTree<int> tree;
        static void Main(String[] args)
        {
            tree = new RBTree<int>();

            RBNode<int> node1 = new RBNode<int>(9);
            RBNode<int> node2 = new RBNode<int>(8);
            RBNode<int> node3 = new RBNode<int>(6);
            RBNode<int> node4 = new RBNode<int>(7);
            RBNode<int> node5 = new RBNode<int>(5);
            RBNode<int> node6 = new RBNode<int>(3);
            RBNode<int> node7 = new RBNode<int>(4);
            RBNode<int> node8 = new RBNode<int>(2);
            RBNode<int> node9 = new RBNode<int>(1);


            tree.InsertNode(node1);
            Console.WriteLine("After adding a 9");
            tree.print();
            tree.InsertNode(node2);
            Console.WriteLine("After adding a 8");
            tree.print();
            tree.InsertNode(node3);
            Console.WriteLine("After adding a 6");
            tree.print();
            tree.InsertNode(node4);
            Console.WriteLine("After adding a 7");
            tree.print();
            tree.InsertNode(node5);
            Console.WriteLine("After adding a 5");
            tree.print();
            tree.InsertNode(node6);
            Console.WriteLine("After adding a 3");
            tree.print();
            tree.InsertNode(node7);
            Console.WriteLine("After adding a 4");
            tree.print();
            tree.InsertNode(node8);
            Console.WriteLine("After adding a 2");
            tree.print();
            tree.InsertNode(node9);
            Console.WriteLine("After adding a 1");
            tree.print();

            Console.WriteLine("Detailed information:");
            Print(tree.root);
            Console.ReadLine();
        }

        public static void Print(RBNode<int> node)
        {
            if (node.parent != null)
            {
                Console.WriteLine("Parent: " + node.parent.data);
            }
            Console.WriteLine("Node data: " + node.data);
            if (node.left != null)
            {
                Console.WriteLine("Left child node: " + node.left.data);
            }
            if (node.right != null)
            {
                Console.WriteLine("Right child node: " + node.right.data);
            }
            Console.WriteLine("Color: " + node.color);
            Console.ReadLine();
            // Recursion call to next node, traverses the tree to the left first
            if (node.left != null)
            {
                Print(node.left);
            }
            if (node.right != null)
            {
                Print(node.right);
            }
        }
    }
}
